def longestWord(words):
    words.sort()
    record = set()
    res = ''
    for word in words:
        if word[:-1] in record or word[:-1] == '':
            if len(word) > len(res):
                res = word
            record.add(word)
    return res


words = ["a", "banana", "app", "appl", "ap", "apple", "apply"]
print(longestWord(words))
